package com.blogs.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blogs.component.BaseActivity;
import com.blogs.component.GetMoreListView;
import com.blogs.component.GetMoreListView.OnMoreListener;
import com.blogs.component.HomeAdapter;
import com.blogs.entity.AppEnums;
import com.blogs.entity.Author;
import com.blogs.entity.Feed;
import com.blogs.service.DeleteBlog;
import com.blogs.service.GetAuthorArticle;
import com.blogs.service.GetUserInfo;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.ImageDownLoad;
import com.blogs.tools.LoadingGif;
import com.cnblogs.R;

public class AuthorHomePage extends BaseActivity implements OnItemClickListener,OnItemLongClickListener{
	enum getDataMethod{
		init,//初始化
		more,//点击更多
	}//获取数据方式
	private DBSharedPreferences sp;
	private String avatar;
	private String blogapp;
	private String name;
	private String postcount;
	private String updated;
	
	private boolean ismoreing;
	private int curpage=1;
	
	private RelativeLayout title_top_bg;
	private LinearLayout author_home_page_root;
	private TextView title_include_title;
	private Button title_include_back;
	private ImageView author_home_page_head_iv;
	private TextView author_home_page_username;
	private TextView author_home_page_blogapp_tv;
	private TextView author_home_page_postcount_tv;
	private TextView author_home_page_website_tv;
	private LinearLayout author_home_list_body;
	private LinearLayout author_home_page_title_ll;
	
    private GetMoreListView listView;
    private LoadingGif loadingGif;
    private HomeAdapter adapter;
    private ArrayList<Feed> feedList;
    private ProgressDialog proDdialog;
    private Toast toast;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.author_home_page);
		sp=new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		author_home_page_root=(LinearLayout)findViewById(R.id.author_home_page_root);
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_title.setText("个人主页");
		title_include_back=(Button)headview.findViewById(R.id.title_include_back);
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});;
		
		avatar=getIntent().getStringExtra("avatar");
		blogapp=getIntent().getStringExtra("blogapp");
		name=getIntent().getStringExtra("name");
		postcount=getIntent().getStringExtra("postcount");
		updated=getIntent().getStringExtra("updated");

		LayoutInflater inflater = LayoutInflater.from(this);
		ViewGroup top_view=(ViewGroup)inflater.inflate(R.layout.home_page_head_view, null);
		author_home_page_head_iv=(ImageView)top_view.findViewById(R.id.author_home_page_head_iv);
		author_home_page_username=(TextView)top_view.findViewById(R.id.author_home_page_username);
		author_home_page_blogapp_tv=(TextView)top_view.findViewById(R.id.author_home_page_blogapp_tv);
		author_home_page_postcount_tv=(TextView)top_view.findViewById(R.id.author_home_page_postcount_tv);
		author_home_page_website_tv=(TextView)top_view.findViewById(R.id.author_home_page_website_tv);
		author_home_page_title_ll=(LinearLayout)top_view.findViewById(R.id.author_home_page_title_ll);
		author_home_list_body=(LinearLayout)findViewById(R.id.author_home_list_body);
		if(name!=null)
		{
			author_home_page_title_ll.setVisibility(View.VISIBLE);
			if(!avatar.equals(""))
			{
				ImageDownLoad.loadDrawable(avatar, author_home_page_head_iv);
			}
			author_home_page_username.setText(name);
			author_home_page_blogapp_tv.setText(updated.substring(0, updated.lastIndexOf(":")));
			author_home_page_postcount_tv.setText("共"+postcount+"篇博文");
			author_home_page_website_tv.setText("http://www.cnblogs.com/"+blogapp+"/");
		}
		else
		{
			requestUserInfo();
		}
		
		listView=new GetMoreListView(this);
		listView.addHeaderView(top_view,null,false);
		listView.setonMoreListener(new OnMoreListener() {
			
			public void onGetMore() {
				// TODO Auto-generated method stub
				if(!ismoreing)
				{
					GetMoreData();
				}
			}
		});
		
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
		loadingGif=new LoadingGif(this, listView, author_home_list_body,sp);
		loadingGif.setOnReloadListener(new LoadingGif.OnReloadListener() {
			
			public void onClick() {
				// TODO Auto-generated method stub
				requestData();
			}
		});
		this.requestData();
		this.InitSkin();
		this.InitProDailog();
	}
	private void InitProDailog()
	{
		proDdialog = new ProgressDialog(this);
		proDdialog.setIndeterminate(true);
		proDdialog.setCancelable(true);
		proDdialog.setMessage("正在删除...");
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			author_home_page_root.getBackground().setLevel(2);
			author_home_page_title_ll.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			author_home_page_root.getBackground().setLevel(1);
			author_home_page_title_ll.getBackground().setLevel(1);
		}
	}
	private void requestUserInfo()
	{
		new GetUserInfo(blogapp, new GetUserInfo.IMsgCallBack() {
			
			public void onSuccess(Author info) {
				// TODO Auto-generated method stub
				author_home_page_title_ll.setVisibility(View.VISIBLE);
				if(!info.avatar.equals(""))
				{
					ImageDownLoad.loadDrawable(info.avatar, author_home_page_head_iv);
				}
				author_home_page_username.setText(info.name);
				author_home_page_blogapp_tv.setText(info.updated.substring(0, info.updated.lastIndexOf(":")));
				author_home_page_postcount_tv.setText("共"+info.postcount+"篇博文");
				author_home_page_website_tv.setText("http://www.cnblogs.com/"+info.blogapp+"/");
			}
			
			public void onFailed() {
				// TODO Auto-generated method stub
				
			}
		}).loadData();
	}
	private void requestData()
	{
		loadingGif.showLoading();
		this.sendTimeLine(getDataMethod.init);
	}
	private void GetMoreData()
	{
		curpage++;
		this.sendTimeLine(getDataMethod.more);
	}
	public void updateRequestData(getDataMethod gdm)
	{
		if(adapter!=null)
		{
			adapter.notifyDataSetChanged();
			if(gdm==getDataMethod.init)
			{
				listView.setAdapter(adapter);
			}
		}
		else
		{
			adapter=new HomeAdapter(this, feedList);
			listView.setAdapter(adapter);
		}

		loadingGif.showData();
	}
	private void sendTimeLine(final getDataMethod gdm)
	{
		new GetAuthorArticle(curpage, blogapp, new GetAuthorArticle.IMsgCallBack() {
			
			public void onSuccess(ArrayList<Feed> feeds) {
				// TODO Auto-generated method stub
				if(feedList==null)
					feedList=new ArrayList<Feed>();
				if(gdm==getDataMethod.init)
				{
					feedList.clear();
				}
				for(int i=0;i<feeds.size();i++)
				{
					feedList.add(feeds.get(i));
				}
				if(gdm==getDataMethod.more)
				{
					ismoreing=false;
					listView.onMoreComplete();
				}
				updateRequestData(gdm);
			}
			
			public void onFailed() {
				// TODO Auto-generated method stub
				loadingGif.showFiale();
			}
		}).loadData();
	}
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if(position>feedList.size())
			return;
		Intent intent=new Intent();
		intent.setClass(this, ContentActivity.class);
		intent.putExtra("Id", feedList.get(position-1).blog_id);
		intent.putExtra("Title", feedList.get(position-1).title);
		intent.putExtra("Author", feedList.get(position-1).author);
		intent.putExtra("publicDate", feedList.get(position-1).public_time);
		intent.putExtra("ViewCount", feedList.get(position-1).hit);
		intent.putExtra("CommentCount", feedList.get(position-1).comment);
		intent.putExtra("url", feedList.get(position-1).blog_url);
		intent.putExtra("blogapp", feedList.get(position - 1).blogapp);
		intent.putExtra("type",AppEnums.EnumContentType.blog);
		startActivity(intent);
	}
	public boolean onItemLongClick(AdapterView<?> parent, View view,final int position,
			long id) {
		// TODO Auto-generated method stub
		String[] items = { "查看正文", "查看评论", "取消" };
		String[] items_d = { "查看正文", "查看评论", "删除博文", "取消" };
		final String[] f_items;
		if(!sp.GetNickName().equals("")&&sp.GetUserID().equals(blogapp))
		{
			f_items=items_d;
		}
		else
		{
			f_items =items;
		}
		
		new AlertDialog.Builder(AuthorHomePage.this).setItems(f_items, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if(f_items[which].equals("查看正文"))
				{
					Intent intent_con = new Intent();
					intent_con.setClass(AuthorHomePage.this, ContentActivity.class);
					intent_con.putExtra("Id", feedList.get(position - 1).blog_id);
					intent_con.putExtra("Title", feedList.get(position - 1).title);
					intent_con.putExtra("Author", feedList.get(position - 1).author);
					intent_con.putExtra("publicDate", feedList.get(position - 1).public_time);
					intent_con.putExtra("ViewCount", feedList.get(position - 1).hit);
					intent_con.putExtra("CommentCount", feedList.get(position - 1).comment);
					intent_con.putExtra("url", feedList.get(position - 1).blog_url);
					intent_con.putExtra("blogapp", feedList.get(position - 1).blogapp);
					intent_con.putExtra("type", AppEnums.EnumContentType.blog);
					startActivity(intent_con);
				}
				else if(f_items[which].equals("查看评论"))
				{
					Intent intent_com = new Intent();
					intent_com.setClass(AuthorHomePage.this, CommentActivity.class);
					intent_com.putExtra("comment_id", feedList.get(position - 1).blog_id);
					intent_com.putExtra("Title", feedList.get(position - 1).title);
					intent_com.putExtra("type", AppEnums.EnumContentType.blog);
					startActivity(intent_com);
				}
				else if(f_items[which].equals("删除博文"))
				{
					new AlertDialog.Builder(AuthorHomePage.this)  
					.setTitle("删除博文") 
					.setMessage("文章删除后将不能回复\n是否确定要删除?") 
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							proDdialog.show();
							new DeleteBlog(sp.GetSessionID(), feedList.get(position-1).blog_id+"", new DeleteBlog.IMsgCallBack() {
								
								public void onSuccess() {
									// TODO Auto-generated method stub
									showToast("删除博文成功");
									feedList.remove(position-1);
									adapter.notifyDataSetChanged();
									proDdialog.hide();
								}
								
								public void onFailed(String err) {
									// TODO Auto-generated method stub
									showToast("删除博文失败,"+err);
									proDdialog.hide();
								}
							}).Del();
						}
					}) 
					.setNegativeButton("取消", null) 
					.show();
				}
				else
				{
					
				}
				
				
			}
		}).create().show();
		return false;
	}
	private void showToast(String text)
	{
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
}
